package py.com.alfa.escriba.web.services.impl;

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;

import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dao.FormulariosDAO;
import py.com.alfa.escriba.persistence.dto.FormulariosDTO;
import py.com.alfa.escriba.util.Utils;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.services.FormulariosService;

public class FormulariosServiceImpl implements FormulariosService{

  private FormulariosDAO formulariosDAO;

  public void setFormulariosDAO(FormulariosDAO formulariosDAO) {
    this.formulariosDAO = formulariosDAO;
  }

  @Override
  public List<FormulariosDTO> listFormularios(InvocationContext ic) throws ServiceException {
    List<FormulariosDTO> formularioss =  formulariosDAO.listFormularios();

    return formularioss;
  }

  @Override
  public FormulariosDTO getFormularios(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    FormulariosDTO formularios =  formulariosDAO.getFormulariosById(id);

    if (formularios == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"formularios.notfound", id);

    return formularios;
  }

  @Override
  public void deleteFormularios(Long id, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    FormulariosDTO formularios =  formulariosDAO.getFormulariosById(id);

    if(formularios == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"formularios.notfound", id);

    try {
      formulariosDAO.deleteFormularios(formularios);
    }
    catch (DataIntegrityViolationException e){
      throw new ServiceException(ErrorCode.DATA_INTEGRITY_VIOLATION, "formularios.constraint.violation", id);
    }

  }

  @Override
  public FormulariosDTO updateFormularios(Long id, FormulariosDTO dto, InvocationContext ic) throws ServiceException {
    if (id == null || id <= 0)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","id");

    if (null == dto)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    FormulariosDTO formularios = formulariosDAO.getFormulariosById(id);

    if (formularios == null)
      throw new ServiceException(ErrorCode.NO_DATA_FOUND,"formularios.notfound", id);

    Utils.validarDTO(dto);

    formulariosDAO.updateFormularios(dto);

    return dto;
  }

  @Override
  public FormulariosDTO createFormularios(FormulariosDTO dto, InvocationContext ic) throws ServiceException {
    if (dto == null)
      throw new ServiceException(ErrorCode.PARAMETER_ERROR,"parameter.error.null","dto");

    try {

      Utils.validarDTO(dto);

      formulariosDAO.createFormularios(dto);
    }
    catch (DuplicateKeyException e){
      throw new ServiceException(ErrorCode.DUPLICATE_KEY_EXCEPTION, "formularios.duplicate.key");
    }

    return dto;
  }

}